import { createSlice } from "@reduxjs/toolkit";
//RTK的切片数据，用与保存多个state数据并给各个组件使用
const tabSlice = createSlice({
    name: 'tab',
    //初始state值
    initialState: () => {
        return [];

    },
    reducers: {
        //修改state方法,添加tab标签item
        addtabitemState(state, action) {
            //判断原来是否已经存在item，如没有才添加
            if (state.findIndex(t => t.key === action.payload.key) === -1) {
                state.push({
                    key: action.payload.key,
                    label: action.payload.label,
                    children: action.payload.children
                })
            }


        },
        deltabitemState(state, action) {
            //找出原来tabitem里对应的key的index
            const delindex = state.findIndex(t => t.key === action.payload.key);
            //筛选出除了要删除的key的其他的tabitem
            state.splice(delindex, 1);

        }
    }

});
export default tabSlice;
export const { addtabitemState, deltabitemState } = tabSlice.actions;

